package capricornus.components.singleselectgroup
{
	import capricornus.components.sprite.AbstractComponent;
	
	import flash.display.DisplayObject;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	[Event(name="selectedChange", type="capricornus.components.singleselectgroup.GroupEvent")]
	[Event(name="overSelectedChange", type="capricornus.components.singleselectgroup.GroupEvent")]
	public class SingleSelectGroup extends EventDispatcher
	{
		private var _currentSelectedItem:AbstractComponent = null;
		
		private var _currentOverSelectedItem:AbstractComponent = null;
		
		private var _arrBDO:Array;
		private var _selectFirsh:Boolean;
		
		public function SingleSelectGroup( selectFirst:Boolean=true )
		{
			_selectFirsh = selectFirst;
			init();
		}
		
		private function init():void
		{
			_arrBDO = new Array();
		}
		
		public function destroy():void
		{
			removeAllItem();
			_arrBDO = null;
		}
		
		public function addItem( value:AbstractComponent ):void
		{
			value.index = _arrBDO.length;
			_arrBDO.push( value );
			value.mouseChildren = false;
			
			if( _selectFirsh && _currentSelectedItem==null )
			{
				selectedItem = value;
			}
			
			value.addEventListener( MouseEvent.CLICK, onClick, false, 0, true );
			value.addEventListener( MouseEvent.ROLL_OVER, onMouseOver, false, 0, true );
			value.addEventListener( MouseEvent.ROLL_OUT, onMouseOut, false, 0, true );
		}
		
		public function removeItem( value:AbstractComponent ):void
		{
			var len:int = _arrBDO.length;
			for( var i:int=0; i<len; i++ )
			{
				if( _arrBDO[i]==value )
				{
					_arrBDO.splice( i, 1 );
					value.removeEventListener( MouseEvent.CLICK, onClick );
					value.removeEventListener( MouseEvent.ROLL_OVER, onMouseOver );
					value.removeEventListener( MouseEvent.ROLL_OUT, onMouseOut );
				}
			}
			if( value==_currentOverSelectedItem )
			{
				_currentOverSelectedItem = null;
			}
			if( value==_currentSelectedItem )
			{
				_currentSelectedItem = null;
			}
		}
		
		public function removeAllItem():void
		{
			var i:int;
			var len:int = _arrBDO.length;
			while( _arrBDO.length > 0 )
			{
				var value:AbstractComponent = _arrBDO.pop();
				value.removeEventListener( MouseEvent.CLICK, onClick );
				value.removeEventListener( MouseEvent.ROLL_OVER, onMouseOver );
				value.removeEventListener( MouseEvent.ROLL_OUT, onMouseOut );
			}
			_currentSelectedItem = null;
		}
		
		public function get selectedItem():AbstractComponent
		{
			return _currentSelectedItem;
		}
		
		public function get overSelectedItem():AbstractComponent
		{
			return _currentOverSelectedItem;
		}
		
		/**
		 * 注意，如果 _currentOverSelectedItem并没有设置index的值，那么返回将会是不可靠的
		 * @return 
		 * 
		 */		
		public function get overSelectedIndex():int
		{
			return _currentOverSelectedItem != null ? _currentOverSelectedItem.index : -1;
		}
		
		public function get selectedIndex():int
		{
			return _currentSelectedItem != null ? _currentSelectedItem.index : -1;
		}
		
		public function set selectedIndex( value:int ):void
		{
			var bdo:AbstractComponent = _arrBDO[value];
			if( bdo != null )
			{
				changeSelected( bdo );
			}
			else 
			{
				if( _currentSelectedItem != null )
				{
					_currentSelectedItem.selected = false;
					_currentSelectedItem = null;
				}
				if( _currentOverSelectedItem != null )
				{
					_currentOverSelectedItem.overSelected = false;
					_currentOverSelectedItem = null;
				}
			}
		}
		
		public function set selectedItem( ac:AbstractComponent ):void
		{
			changeSelected( ac );
		}
		
		private function changeSelected( value:AbstractComponent ):void
		{
			if( value==_currentSelectedItem ) return;
			
			if( _currentSelectedItem != null )
			{
				_currentSelectedItem.selected = false;
			}
			_currentSelectedItem = value;
			_currentSelectedItem.selected = true;
			
			dispatchEvent( new GroupEvent( GroupEvent.SELECTED_CHANGE, _currentSelectedItem.index ) );
		}
		
		private function onClick( e:MouseEvent ):void
		{
			var bdo:AbstractComponent = e.currentTarget as AbstractComponent;
			changeSelected( bdo );
		}
		
		private function changeOverSelected( value:AbstractComponent ):void
		{
			if( value==_currentOverSelectedItem ) return;
			
			if( _currentOverSelectedItem != null )
			{
				_currentOverSelectedItem.overSelected = false;
			}
			_currentOverSelectedItem = value;
			_currentOverSelectedItem.overSelected = true;
			
			dispatchEvent( new GroupEvent( GroupEvent.OVER_SELECTED_CHANGE, _currentOverSelectedItem.index ) );
		}
		
		private function onMouseOver( e:MouseEvent ):void
		{
			var bdo:AbstractComponent = e.currentTarget as AbstractComponent;
			if( bdo==_currentSelectedItem ) return;
			
			changeOverSelected( bdo );
		}
		
		private function onMouseOut( e:MouseEvent ):void
		{
			var bdo:AbstractComponent = e.currentTarget as AbstractComponent;
			if( bdo==_currentSelectedItem ) return;
			
			if( _currentOverSelectedItem==bdo )
			{
				_currentOverSelectedItem = null;
			}
			bdo.overSelected = false;
		}
	}
}


